const app = getApp()

Page({
    data: {
        progress: 0,
        interval: null
    },
    onLoad(options) {
        this.data.interval = setInterval(() => {
            if (this.data.progress <= 99) {
                this.setData({
                    progress: this.data.progress + 1
                })
            } else {
                clearInterval(this.data.interval)
                this.uploadCheckImage(options)
            }
        }, 50)
    },
    uploadCheckImage(options) {
        console.log(wx.getStorageSync('face_user_id'));
        
        app.request(app.apiDomain + 'index.php?c=face&a=face', {
            face_user_id: wx.getStorageSync('face_user_id'),
            face_id: 1,
            image: options.image
        }, (res) => {
            if (res.data.status == '1') {
                let orderId = res.data.data.id
                this.goToResultPage(orderId)
            } else {
                wx.showToast({
                    title: '检查失败',
                    icon: 'none',
                    image: '',
                    duration: 1500,
                    mask: true,
                })
                setTimeout(() => {
                    this.checkRecord()
                }, 1500)
            }
        })
    },
    onHide() {
        clearInterval(this.data.interval)
    },
    goToResultPage(orderId) {
        wx.reLaunch({
            url: `result?orderId=${orderId}`
        })
    },
    checkRecord() {
        wx.navigateTo({
            url: 'history'
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        return {
        	path: '/pages/skin/index',
        	title: '我的AI皮肤健康报告出来啦~,你也来试试？'
        }
    }
})
